<template>
  <div class="app-container">
    <div class="explain">在邀请列表中审核通过即表示邀请成功，奖励金额发放至用户余额中。</div>
    <div class="spaceline" />
    <el-form
      ref="form"
      v-loading="infoLoading"
      class="common-form"
      :model="form"
      label-width="120px"
      :rules="rules"
      :inline-message="true"
    >
      <el-form-item label="是否开启">
        <el-switch v-model="form.is_open" />
      </el-form-item>
      <el-form-item label="发放金额">
        <el-input v-model="form.price" />
        <span class="smalltip">
            <i class="el-icon-info" />
            成功邀请1人可获得的奖励
        </span>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="onSubmit('form')">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { setConfig } from '@/api/configuration'

export default {
  data() {
    return {
      infoLoading: true,
      form: {
        is_open: false,
        price: '1',
      },
      rules: {}
    }
  },
  created() {
    this.fetchInfo()
  },
  methods: {
    fetchInfo() {
      this.infoLoading = true
      const param = {}
      setConfig(param, 'get')
        .then(response => {
          if(response.data.invite_register.is_open == 1){
              this.form.is_open = true;
          }else{
              this.form.is_open = false;
          }
          this.form.price = response.data.invite_register.price;
          this.infoLoading = false
        })
        .catch(() => {})
    },
    onSubmit(formName) {
      const insertData = { ...this.form }
      this.$refs[formName].validate(valid => {
        if (valid) {
          insertData.is_open = insertData.is_open === true ? 1 : 0
          setConfig({'invite_register':insertData})
            .then(response => {
              this.$store.dispatch('config/getConfigInfo')
              this.$message.success(response.message)
              return true
            })
            .catch(() => {})
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style scoped>
.explain{
  padding: 20px 16px;
  background-color: #fdf6ec;
  border-radius: 4px;
  border-left: 5px solid #f9c77c;
  margin: 0 0 20px;
  font-size: 14px;
  color: #e6a23c;
  line-height: 1em;
}
.el-input,
.el-input-group {
  width: 50%;
}
</style>
